package com.android.hedgehog;

import java.util.List;

import android.graphics.Color;

import com.android.hedgehog.FreePlayScreen.GameStatus;
import com.android.hedgehog.RisingConnect.GameState;
import com.android.hedgehog.bots.Bot;
import com.android.hedgehog.bots.Bot.Phase;
import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Screen;
import com.android.hedgehog.framework.Input.TouchEvent;
import com.android.hedgehog.puzzles.GoalieScreen;
import com.android.hedgehog.puzzles.PuzzleScreen;

public class BattleScreen extends Screen {
	enum GameStatus { // the game is always in one of these states
		ReadyHuman, ChooseHuman, AnswerHuman, ResultHuman, ReadyBot, ChooseBot, AnswerBot, ResultBot, Paused, GameOver
	}

	GameStatus state, prevState;
	BattleEngine puzzleEngine;
	BotEngine botEngine;
	Bot bot;
	double totalTime;
	double currentTime;
	double[] bufferTime = { 0.0, 3.0, 3.0, 2.0, 10.0, 2.0, 2.0, 2.0 };// robot-movement,readyHuman,readyBot,chooseBot,answerBot,resultHuman,resultBOT,GAMEOVER
	boolean swip;
	boolean win = false; // if player wins=true
	int level;
	int len;
	int roundNumber; // which bot/level your in
	int turnNumber; // 1st turn: human player chooses.

	public Graphics graphics;

	public BattleScreen(Game game, int level) {
		super(game);
		roundNumber = level;
		puzzleEngine = new BattleEngine(game);
		botEngine = new BotEngine(game, level);
		bot = new Bot(game, level);
		graphics = game.getGraphics();
		state = GameStatus.ReadyHuman;

	}

	@Override
	public void update(float deltaTime) {
		if (state != GameStatus.Paused) {
			totalTime += deltaTime;
		}

		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();

		if (state != GameStatus.Paused)
			updateRunning(touchEvents, deltaTime); // MAIN UPDATE
		if (state == GameStatus.Paused)
			updatePaused(touchEvents);
		if (state == GameStatus.ReadyHuman)
			updateReadyHuman(touchEvents);
		if (state == GameStatus.ChooseHuman)
			updateChooseHuman(touchEvents);
		if (state == GameStatus.AnswerHuman) {
			updateAnswerHuman(touchEvents, deltaTime);
		}
		if (state == GameStatus.ResultHuman) {
			updateResultHuman(touchEvents);
		}
		if (state == GameStatus.ReadyBot) {
			updateReadyBot(touchEvents);
		}
		if (state == GameStatus.ChooseBot) {
			updateChooseBot(touchEvents);
		}
		if (state == GameStatus.AnswerBot) {
			updateAnswerBot(touchEvents);
		}
		if (state == GameStatus.ResultBot) {
			updateResultBot(touchEvents);
		}
		if (state == GameStatus.GameOver) {
			updateGameOver();
		}

	}

	private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {// RUNNING

		// botEngine.bot.update(deltaTime);

		len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) { // TOUCH UP

				if (event.x < 30 && event.y < 30) { // Pause Button
					if (Settings.soundEnabled)
						Assets.click.play(1);
					prevState = state;
					state = GameStatus.Paused; // goto Paused state
					return;
				} // Pause Button

			}// TOUCH UP;
		}//FOR
		
		bot.update(touchEvents, deltaTime);

	}// **********************UPDATE RUNNING**********************

	private void updatePaused(List<TouchEvent> touchEvents) {

		len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) { // TOUCH UP

				if (event.y > 135 && event.y < 185) { // RESUME
					if (Settings.soundEnabled)
						Assets.click.play(1);
					state = prevState;
					return;
				} // RESUME

				if (event.x > 70 && event.x < 245 && event.y > 255 && event.y < 305) { // QUIT
					game.setScreen(new MainMenu2(game));
					if (Settings.soundEnabled)
						Assets.click.play(1);
					return;
				}// QUIT

			}
		}

	}// ***************************UPDATE PAUSED******************

	private void updateReadyHuman(List<TouchEvent> touchEvents) {

		if (totalTime > currentTime + bufferTime[1]) {
			puzzleEngine.chooseGameForRound();
			 state = GameStatus.ChooseHuman; // CORRECT
			//state = GameStatus.ReadyBot; // test
			turnNumber++;
		}

	}// **************************UPDATE READYHUMAN*****************

	private void updateChooseHuman(List<TouchEvent> touchEvents) {// CHOOSEHUMAN

		len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) { // TOUCH UP

				if (event.y > 354) { // hit 1-10 box
					if (event.y < 418) {// 1-5
						if (event.x < 64) {
							level = 1;
						}
						if (event.x > 64 && event.x < 128) {
							level = 2;
						}
						if (event.x > 128 && event.x < 192) {
							level = 3;
						}
						if (event.x > 192 && event.x < 256) {
							level = 4;
						}
						if (event.x > 256 && event.x < 320) {
							level = 5;
						}
					}// 1-5
					if (event.y > 418) {// 6-10
						if (event.x < 64) {
							level = 6;
						}
						if (event.x > 64 && event.x < 128) {
							level = 7;
						}
						if (event.x > 128 && event.x < 192) {
							level = 8;
						}
						if (event.x > 192 && event.x < 256) {
							level = 9;
						}
						if (event.x > 256 && event.x < 320) {
							level = 10;
						}
					}// 6-10

					if (Settings.soundEnabled)
						Assets.click.play(1);
					state = GameStatus.AnswerHuman;
					puzzleEngine.startPuzzle(level, totalTime);
					return;
				} // hit 1-10 box

			}// TOUCH UP;
		}

	}// *************************UPDATE CHOOSEHUMAN********************

	private void updateAnswerHuman(List<TouchEvent> touchEvents, float deltaTime) {

		puzzleEngine.updatePuzzle(touchEvents, deltaTime);
		if (puzzleEngine.turnFinished) {
			currentTime = totalTime;
			state = GameStatus.ResultHuman;
			// puzzleEngine.startPuzzle(5, totalTime);
		}

	}

	private void updateResultHuman(List<TouchEvent> touchEvents) {

		if (puzzleEngine.playerScore > 14) {
			win = true;
			state = GameStatus.GameOver;
			return;
		}

		if (totalTime > currentTime + bufferTime[5]) {
			currentTime = totalTime;
			state = GameStatus.ReadyBot;
		}
	}

	private void updateReadyBot(List<TouchEvent> touchEvents) {

		if (totalTime > currentTime + bufferTime[2]) {
			currentTime = totalTime;
			bot.selectPointsToPlay(); // !!!
			state = GameStatus.ChooseBot;
		}

	}// *******************UPDATE READYBOT************

	private void updateChooseBot(List<TouchEvent> touchEvents) {
		if (totalTime > currentTime + bufferTime[3]) {
			currentTime = totalTime;
			bot.phase = Phase.Compute; // !!!
			state = GameStatus.AnswerBot;
		}
	}

	private void updateAnswerBot(List<TouchEvent> touchEvents) {

		/*
		 * len = touchEvents.size(); for (int i = 0; i < len; i++) {// FOR
		 * TouchEvent event = touchEvents.get(i); if (event.type ==
		 * TouchEvent.TOUCH_DOWN || event.type == TouchEvent.TOUCH_DRAGGED) {
		 * bot.highlighted=true; } if (event.type == TouchEvent.TOUCH_UP) { //
		 * TOUCH UP bot.ifHit(event.x, event.y); bot.highlighted=false; break; }
		 * 
		 * }
		 */

		if (bot.phase == Phase.Respond) {
			currentTime = totalTime;
			state = GameStatus.ResultBot;
		}
		// if (totalTime > currentTime + bufferTime[4]) {
		// currentTime = totalTime;
		// state = GameStatus.ResultBot;
		// }
	}

	private void updateResultBot(List<TouchEvent> touchEvents) {

		if (bot.botScore > 14) {
			state = GameStatus.GameOver;
			return;
		}

		if (totalTime > currentTime + bufferTime[6]) {
			bot.phase = Phase.Standby;
			currentTime = totalTime;
			state = GameStatus.ReadyHuman;
		}

	}

	private void updateGameOver() {
		if (totalTime > currentTime + bufferTime[7]) {
			game.setScreen(new BattleResultScreen(game, roundNumber, win, puzzleEngine.playerScore,
					turnNumber));
		}
	}

	@Override
	public void present(float deltaTime) {// -------------PRESENT
		if (state != GameStatus.Paused)
			presentRunning();
		if (state == GameStatus.Paused)
			presentPaused();
		if (state == GameStatus.ReadyHuman)
			presentReadyHuman();
		if (state == GameStatus.ChooseHuman)
			presentChooseHuman();
		if (state == GameStatus.AnswerHuman)
			presentAnswerHuman();
		if (state == GameStatus.ResultHuman) {
			presentResultHuman();
		}
		if (state == GameStatus.ReadyBot) {
			presentReadyBot();
		}
		if (state == GameStatus.ChooseBot) {
			presentChooseBot();
		}
		if (state == GameStatus.AnswerBot) {
			presentAnswerBot();
		}
		if (state == GameStatus.ResultBot) {
			presentResultBot();
		}
		if (state == GameStatus.GameOver) {
			presentGameOver();
		}

	}// ---------------------------------------------------PRESENT

	private void presentRunning() {// ALWAYS RUNNING
		graphics.drawPixmap(Assets.battlescreenbackground, 0, 0);
		graphics.drawPixmap(Assets.blackbuttons, 0, 0, 0, 0, 30, 30);// pause
		graphics.drawPixmap(Assets.blackbuttons, 290, 0, 30, 0, 30, 30);// restart
		graphics.drawLine(0, 31, 320, 31, Color.BLACK);

		// botEngine.bot.present();
		bot.present();

		graphics.drawRect2(20, 40, 40, 40, Color.BLACK);
		graphics.drawUniversalText(bot.name, 2, false, 65, 55, 16, 255, Color.BLACK);
		graphics.drawSpecialText("" + bot.botScore, 40, 70, 24, 2, Color.BLACK); // robotScore

		graphics.drawPixmap(Assets.human, 0, 210);// player
		graphics.drawSpecialText("challenger", 215, 302, 18, 2, Color.BLACK);
		graphics.drawRect2(270, 282, 40, 40, Color.BLACK);
		graphics.drawSpecialText("" + puzzleEngine.playerScore, 290, 310, 24, 2, Color.BLACK); // playerScore
	}

	private void presentPaused() {
		graphics.drawPixmap(Assets.blackbackground, 0, 0);
		graphics.drawSpecialText("RESUME", 160, 180, 80, 2, Color.GREEN);
		graphics.drawSpecialText("QUIT", 160, 300, 80, 2, Color.RED);
	}

	private void presentReadyHuman() {

		// graphics.drawText(totalTime + "", 160, 200, 8, Color.WHITE); // test

		if ((-100 + (int) ((totalTime - currentTime) * 150)) < 100) {// HUMAN'S
																		// TURN
			graphics.drawText("Human's turn", -100 + ((int) ((totalTime - currentTime) * 150)), 150, 22,
					Color.RED);
		} else {
			graphics.drawText("Human's turn", 100, 150, 22, Color.RED);
		}// HUMAN's TURN

	}

	private void presentChooseHuman() {

		// graphics.drawText(totalTime + "", 160, 200, 12, Color.WHITE); test
		graphics.drawSpecialText(puzzleEngine.currentPuzzle, 160, 20, 20, 1, Color.RED);// ROUND
		int k = 1;
		for (int i = 352; i < 440; i = i + 64) {
			for (int j = 0; j < 310; j = j + 64) {
				graphics.drawPixmap(Assets.buttons64, j, i, 0, 0, 64, 64);
				graphics.drawSpecialText("" + k++, j + 32, i + 38, 36, 2, Color.GREEN);
			}
		}

	}

	private void presentAnswerHuman() {
		puzzleEngine.presentPuzzle();
	}

	private void presentResultHuman() {
		graphics.drawSpecialText(puzzleEngine.currentPuzzle, 160, 20, 20, 1, Color.RED);// ROUND
		if (puzzleEngine.success)
			graphics.drawText("+ " + level + " Scored", 100, 140, 18, Color.RED);
		else
			graphics.drawText("Failed to Score", 100, 140, 18, Color.RED);
	}

	private void presentReadyBot() {
		graphics.drawSpecialText(puzzleEngine.currentPuzzle, 160, 20, 20, 1, Color.RED);// ROUND
		if ((-100 + (int) ((totalTime - currentTime) * 150)) < 100) {// ROBOT's
																		// turn
			graphics.drawText("Robot's turn", -100 + ((int) ((totalTime - currentTime) * 150)), 150, 22,
					Color.RED);
		} else {
			graphics.drawText("Robot's turn", 100, 150, 22, Color.RED);
		}// ROBOT's turn;
	}

	private void presentChooseBot() {
		graphics.drawSpecialText(puzzleEngine.currentPuzzle, 160, 20, 20, 1, Color.RED);// ROUND
		graphics.drawText("Robot goes for " + bot.selected + " points", 120, 150, 18, Color.RED);
	}

	private void presentAnswerBot() {
		graphics.drawSpecialText(puzzleEngine.currentPuzzle, 160, 20, 20, 1, Color.RED);// ROUND
	}

	private void presentResultBot() {
		if (bot.correct)
			graphics.drawText("Robot scored " + bot.selected + " points", 100, 150, 15, Color.RED);
		else
			graphics.drawText("Robot Missed", 100, 150, 18, Color.RED);
	}

	private void presentGameOver() {
		if (win) {
			graphics.drawText("Winner!", 100, 150, 18, Color.RED);
		} else {
			graphics.drawText("it's over", 100, 150, 18, Color.RED);
		}

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
